package com.koron.css2.projectCenter.impl;

import com.alibaba.fastjson.JSONObject;
import com.koron.css2.baseConfig.bean.PubReceiptBean;
import com.koron.css2.baseConfig.mapper.PubReceiptMapper;
import com.koron.css2.newProjectCenter.bean.ProjectReceiptBean;
import com.koron.css2.newProjectCenter.mapper.ProjectReceiptMapper;
import com.koron.css2.projectCenter.bean.PjConsultBean;
import com.koron.css2.projectCenter.bean.PjInstallBean;
import com.koron.css2.projectCenter.mapper.PjConsultMapper;
import com.koron.css2.projectCenter.mapper.PjInstallMapper;
import com.koron.css2.projectCenter.mapper.PjUseMatrMapper;
import com.koron.css2.projectCenter.service.PjConsultService;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.workflow.ProcessCommit;
import com.koron.css2.workflow.ProcessInterface;
import com.koron.css2.workflow.WorkflowImpl;
import com.koron.css2.workflow.bean.HistoryTaskMsgBean;
import com.koron.css2.workflow.bean.TaskInfoBean;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.List;

/**
 * 用水咨询审批后续操作
 * @author 许海宜
 * 2020年3月25日
 */
public class PjConsultProcess implements ProcessInterface {
	
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
		MessageBean<?> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Void.class);
		try {
			//预算审批后直接到收款审批节点
			if(("sub_pj_consult_budget2".equals(fromNodeCode) || "sub_pj_consult_budget3".equals(fromNodeCode) || 
					"sub_pj_consult_budget4".equals(fromNodeCode)) && operation == OPERATION_AGREE) {
				//查询用水咨询
				PjConsultMapper pjConsultMapper = factory.getMapper(PjConsultMapper.class);
				PjConsultBean pjConsultBean = pjConsultMapper.selectById(billId);
				//插入工程收费
				addProjectReceipt(factory, userInfo, pjConsultBean,new PjInstallBean(),"1");
				
			}
			//收款审批后执行
			if("charge-approval".equals(fromNodeCode) && OPERATION_AGREE == operation) {	//budget-approval
				//查询用水咨询
				PjConsultMapper pjConsultMapper = factory.getMapper(PjConsultMapper.class);
				PjConsultBean pjConsultBean = pjConsultMapper.selectById(billId);
				
				//插入用水安装
				PjInstallBean pjInstallBean = addPjInstall(factory, pjConsultBean, userInfo);
				
				PjInstallMapper pjInstallMapper = factory.getMapper(PjInstallMapper.class);
				
				PjInstallBean updateBean = new PjInstallBean();
				updateBean.setId(pjInstallBean.getId());
				updateBean.setBudgetPayWay(pjConsultBean.getBudgetPayWay());
				updateBean.setBudgetPayStaff(pjConsultBean.getBudgetPayStaff());
				updateBean.setBudgetPayComments(pjConsultBean.getComments());
				pjInstallMapper.saveCharge(pjInstallBean);
				updateBean.setAccruedMoney(pjConsultBean.getBudgetMoney());
				pjInstallMapper.saveAccruedMoney(updateBean);
				//插入用料
				addUseMatr(factory, pjConsultBean.getId(), pjInstallBean.getId());
			}
		}catch (Exception e) {
			factory.close(false);
			logger.error("执行用水咨询审批后续操作失败", e);
			info = MessageBean.create(Constant.MESSAGE_INT_FAIL, "执行用水咨询审批后续操作失败", Void.class);
		}
		return info;
	}

	/**
	 * 新增工程收款单
	 * @param factory
	 * @param pjConsultBean
	 * @param userInfo
	 */
	public void addProjectReceipt(SessionFactory factory, UserInfoBean userInfo, PjConsultBean pjConsultBean, PjInstallBean pjInstallBean,String chargeType) {
		//组装工程收款单
		ProjectReceiptBean projectReceiptBean = new ProjectReceiptBean();
		projectReceiptBean.setId(Tools.getObjectId());
		projectReceiptBean.setBillId(pjConsultBean.getId());
		projectReceiptBean.setBillNo(pjConsultBean.getBillNo());
		projectReceiptBean.setBillDate(CommonUtils.getCurrentDate());
		projectReceiptBean.setBillType(PjConsultService.CONSULT_RECEIPT);
		projectReceiptBean.setReceivableMoney(pjConsultBean.getBudgetMoney()==null?0.0:pjConsultBean.getBudgetMoney());//应收
		projectReceiptBean.setDepositMoney(pjConsultBean.getDepositMoney()==null?0.0:pjConsultBean.getDepositMoney());//押金
		projectReceiptBean.setTotalMoney(pjConsultBean.getBudgetMoney()+(pjConsultBean.getDepositMoney()==null?0.0:pjConsultBean.getDepositMoney()));
		projectReceiptBean.setChargeFlag(0);
		projectReceiptBean.setComments("用水咨询单据编号：" + pjConsultBean.getBillNo());
		projectReceiptBean.setTenantId(userInfo.getCurWaterCode());
		projectReceiptBean.setCreateAccount(userInfo.getUserInfo().getAcount());
		projectReceiptBean.setCreateName(userInfo.getUserInfo().getName());
		projectReceiptBean.setChargeType(chargeType);
		//新增工程收款单
		ProjectReceiptMapper projectReceiptMapper = factory.getMapper(ProjectReceiptMapper.class);
		projectReceiptMapper.add(projectReceiptBean);
	}
	
	/**
	 * 新增用水安装
	 * @param factory
	 * @param pjConsultBean
	 * @param userInfo
	 */
	public PjInstallBean addPjInstall(SessionFactory factory, PjConsultBean pjConsultBean, UserInfoBean userInfo) {
		//组装用水安装
		PjInstallBean pjInstallBean = new PjInstallBean();
		pjInstallBean.setId(Tools.getObjectId());
		pjInstallBean.setBillNo(pjConsultBean.getBillNo());
//		pjInstallBean.setBillNo(Tools.getBillNo(factory, PjConsultService.INSTALL_RECEIPT));
		pjInstallBean.setBillDate(CommonUtils.getCurrentDate());
		pjInstallBean.setConsultId(pjConsultBean.getId());
		pjInstallBean.setAccruedMoney(pjConsultBean.getBudgetMoney());
		pjInstallBean.setTenantId(userInfo.getCurWaterCode());
		pjInstallBean.setCreateName(userInfo.getUserInfo().getName());
		pjInstallBean.setCreateAccount(userInfo.getUserInfo().getAcount());
		pjInstallBean.setConstGroup(pjConsultBean.getInstallStaff());
		pjInstallBean.setSendOrderDate(CommonUtils.getCurrentDate());
		//新增用水安装
		PjInstallMapper pjInstallMapper = factory.getMapper(PjInstallMapper.class);
		pjInstallMapper.add(pjInstallBean);
		return pjInstallBean;
	}
	
	public void addUseMatr(SessionFactory factory, String consultId, String installId) {
		PjUseMatrMapper pjUseMatrMapper = factory.getMapper(PjUseMatrMapper.class);
		pjUseMatrMapper.copy(consultId);
		pjUseMatrMapper.updatePjId(consultId, installId);
	}
	/**
	 * 跳转节点
	 * @return
	 */
	private MessageBean<?> nextProcess(SessionFactory factory, UserInfoBean userInfo, PjConsultBean pjConsultBean){
		MessageBean<?> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Void.class);

		//获取当前任务ID
		PubReceiptMapper pubmapper = factory.getMapper(PubReceiptMapper.class);
		String setCode = pubmapper.getWorkflowApp(userInfo.getCurWaterCode());
		PubReceiptBean receipt = pubmapper.selectPubReceipt(PjConsultService.CONSULT_RECEIPT);
		List<HistoryTaskMsgBean> historyMsgList = WorkflowImpl.workflowhistory(setCode, receipt.getProcessCode(), pjConsultBean.getId());
		int length = historyMsgList.size()==0?0:historyMsgList.size()-1;
		HistoryTaskMsgBean hisTask = historyMsgList.get(length);
		if(hisTask != null) {
			List<TaskInfoBean> taskInfo = hisTask.getTasks();
			if(taskInfo.size() > 0) {
				JSONObject json = new JSONObject();
				json.put("billId", pjConsultBean.getId());
				json.put("taskId", taskInfo.get(0).getTaskId());
				json.put("operation", 32);
				json.put("processCode", receipt.getProcessCode());
				json.put("operator", userInfo.getUserInfo().getAcount());
				json.put("setCode", setCode);
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("user", userInfo.getUserInfo().getAcount());
				json.put("formVariables", map);
				json.put("comment", "");
				json.put("receiptType", receipt.getProcessCode());
				json.put("title", "工程收款"+"("+pjConsultBean.getBillNo()+")");
				RequestBean req = new RequestBean();
				req.setData(json);
				ProcessCommit processCommit = new ProcessCommit();
				info = processCommit.exec(factory, userInfo, req);
			}
		}
		return info;
	}

	@Override
	public MessageBean<?> before(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
		return exec(factory, userInfo, billId, fromNodeCode, operation);
	}

	@Override
	public MessageBean<?> after(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, String toNodeCode) {
		MessageBean<?> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Void.class);
		//预算审批后直接到收款审批节点
		if(("sub_pj_consult_budget2".equals(fromNodeCode) || "sub_pj_consult_budget3".equals(fromNodeCode) || "sub_pj_consult_budget4".equals(fromNodeCode)) && "charge".equals(toNodeCode)) {
			PjConsultMapper pjConsultMapper = factory.getMapper(PjConsultMapper.class);
			PjConsultBean pjConsultBean = pjConsultMapper.selectById(billId);
			info = nextProcess(factory, userInfo, pjConsultBean);
		}
		if("charge-approval".equals(fromNodeCode) && "end".equalsIgnoreCase(toNodeCode)) {
			PjInstallMapper pjInstallMapper = factory.getMapper(PjInstallMapper.class);
			PjInstallBean pjInstall = pjInstallMapper.selectByConsultId(billId);
			JSONObject json = new JSONObject();
			json.put("title", "安装施工"+"(" + pjInstall.getBillNo() + ")");
			json.put("billId", pjInstall.getId());
			json.put("operation", 32);
			json.put("operator", userInfo.getUserInfo().getAcount());
			json.put("formVariables", new HashMap<String, Object>());
			json.put("comment", "安装施工");
			json.put("receiptType", "PJ_INSTALL");
			RequestBean req = new RequestBean();
			req.setData(json);
			ProcessCommit processCommit = new ProcessCommit();
			info = processCommit.exec(factory, userInfo, req);
		}
		return info;
	}
}
